package com.ruanyun.sys.modular.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruanyun.base.pojo.node.ZTreeNode;
import com.ruanyun.base.pojo.page.LayuiPageFactory;
import com.ruanyun.base.pojo.page.LayuiPageInfo;
import com.ruanyun.sys.modular.system.entity.SysDistrict;
import com.ruanyun.sys.modular.system.mapper.SysDistrictMapper;
import com.ruanyun.sys.modular.system.model.params.SysDistrictParam;
import com.ruanyun.sys.modular.system.model.SysDistrictDto;
import com.ruanyun.sys.modular.system.model.result.SysDistrictResult;
import  com.ruanyun.sys.modular.system.service.SysDistrictService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangchuan
 * @since 2019-08-09
 */
@Service
public class SysDistrictServiceImpl extends ServiceImpl<SysDistrictMapper, SysDistrict> implements SysDistrictService {

    @Override
    public void add(SysDistrictParam param){
//        Long id = this.baseMapper.getMaxId();
//        param.setId(id+1);
        //获取path
        Long pId = param.getParentId();
        SysDistrict pEntity = new SysDistrict();
        pEntity.setDistrictId(pId);
        QueryWrapper<SysDistrict> queryWrapper = new QueryWrapper<>(pEntity);
        SysDistrict sysDistrict = this.baseMapper.selectOne(queryWrapper);
        String pPath = sysDistrict.getPath();
        String path = pPath+"."+param.getDistrictCode();
        param.setPath(path);

        SysDistrict entity = getEntity(param);
        this.save(entity);
    }

    /**
     * 新增
     *
     * @author wangchuan
     * @Date 2019-08-09
     */
    @Override
    public SysDistrictDto getSysDistrictDto(SysDistrict sysDistrict){
        SysDistrictDto sysDistrictDto = new SysDistrictDto();
        BeanUtil.copyProperties(sysDistrict, sysDistrictDto);
        SysDistrict pEntity = new SysDistrict();
        pEntity.setDistrictId(sysDistrict.getParentId());
        QueryWrapper<SysDistrict> queryWrapper = new QueryWrapper<>(pEntity);
        SysDistrict entity = this.baseMapper.selectOne(queryWrapper);
        sysDistrictDto.setPName(entity.getDistrictName());
        return sysDistrictDto;
    }

    @Override
    public void delete(SysDistrictParam param){
        this.baseMapper.updateIsDelete(param.getDistrictId());
//        this.removeById(getKey(param));
    }

    @Override
    public void update(SysDistrictParam param){
        SysDistrict oldEntity = getOldEntity(param);
        SysDistrict newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        //获取path
        Long pId = param.getParentId();
        SysDistrict pEntity = new SysDistrict();
        pEntity.setDistrictId(pId);
        QueryWrapper<SysDistrict> queryWrapper = new QueryWrapper<>(pEntity);
        SysDistrict sysDistrict = this.baseMapper.selectOne(queryWrapper);
        if(sysDistrict.getDistrictLevel()!=0){
            String pPath = sysDistrict.getPath();
            String path = pPath+"."+param.getDistrictCode();
            newEntity.setPath(path);
        }
        this.updateById(newEntity);
    }

    @Override
    public SysDistrictResult findBySpec(SysDistrictParam param){
        return null;
    }

    @Override
    public List<SysDistrictResult> findListBySpec(SysDistrictParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(SysDistrictParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(SysDistrictParam param){
        return param.getDistrictId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private SysDistrict getOldEntity(SysDistrictParam param) {
        return this.getById(getKey(param));
    }

    private SysDistrict getEntity(SysDistrictParam param) {
        SysDistrict entity = new SysDistrict();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    /**
     * 获取ztree的节点列表
     *
     * @author administrator
     * @Date 2018/12/23 5:16 PM
     */
    @Override
    public List<ZTreeNode> tree(Long id) {
        List<ZTreeNode> zTreeNodeList = this.baseMapper.tree(id);
        for(ZTreeNode zTreeNode : zTreeNodeList){
            Long disId = zTreeNode.getId();
//            Integer count = this.baseMapper.getCountByPid(disId);
            SysDistrict sysDistrict = this.baseMapper.selectById(disId);
            Integer level = sysDistrict.getDistrictLevel();
            if(level<4){
                zTreeNode.setIsParent(true);
            }else{
                zTreeNode.setIsParent(false);
            }
        }
        return zTreeNodeList;

    }

    /**
     * 获取ztree的节点列表
     *
     * @author administrator
     * @Date 2018/12/23 5:16 PM
     */
    @Override
    public List<ZTreeNode> treeInit() {
        List<ZTreeNode> zTreeNodeList =  this.baseMapper.treeInit();
        for(ZTreeNode zTreeNode : zTreeNodeList){
            Long disId = zTreeNode.getId();
//            Integer count = this.baseMapper.getCountByPid(disId);
            SysDistrict sysDistrict = this.baseMapper.selectById(disId);
            Integer level = sysDistrict.getDistrictLevel();
            if(level<4){
                zTreeNode.setIsParent(true);
            }else{
                zTreeNode.setIsParent(false);
            }
        }
        return zTreeNodeList;
    }

    /**
     * 获取所有部门列表
     *
     * @author administrator
     * @Date 2018/12/23 5:16 PM
     */
    @Override
    public Page<Map<String, Object>> list(String condition, String code,String path) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.list(page, condition, code, path);
    }

    @Override
    public SysDistrict selectId(Long id){
        SysDistrict entity = new SysDistrict();
        entity.setDistrictId(id);
        QueryWrapper<SysDistrict> queryWrapper = new QueryWrapper<>(entity);
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public void updatedddd(SysDistrict sysDistrict){
        this.baseMapper.updataData(sysDistrict);
    }

    @Override
    public void deleteallaaa(){
        this.baseMapper.deleteall();
    }

}
